import hashlib

from django.contrib.auth.models import AbstractUser
from ops.utils.models import table_prefix
from dvadmin.utils.models import CoreModel
from django.db import models
# from mirage import fields

STATUS_CHOICES = (
    (0, "禁用"),
    (1, "启用"),
)


class Instance(CoreModel):
    name = models.CharField(max_length=100, blank=True, null=True, verbose_name="名字", help_text="名字")
    host = models.CharField(max_length=100, blank=True, null=True, verbose_name="主机", help_text="主机")
    user = models.CharField(max_length=100, blank=True, null=True, verbose_name="数据库用户名", help_text="数据库用户名")
    # passwd = fields.EncryptedCharField(verbose_name='密码', max_length=300, default='', blank=True)
    port = models.IntegerField(blank=True, null=True, verbose_name="端口", help_text="端口")
    status = models.IntegerField(blank=True, null=True, verbose_name="状态", help_text="状态")
    project = models.CharField(max_length=100, blank=True, null=True, verbose_name="归属项目", help_text="归属项目")

    STATUS_CHOICES = (
        (0, "禁用"),
        (1, "启用"),
    )

    DB_TYPE_CHOICES = (
        ('mysql', 'MySQL'),
        ('redis', 'Redis'),
        ('mssql', 'MsSQL'),
        ('pgsql', 'PgSQL'),
        ('oracle', 'Oracle'),
        ('mongo', 'Mongo'),
        ('phoenix', 'Phoenix'),
        ('inception', 'Inception'),
        ('goinception', 'goInception'))
    type = models.CharField(max_length=100, default="mysql", choices=DB_TYPE_CHOICES, verbose_name="类型", help_text="类型")
    sort = models.IntegerField(default=1, verbose_name="显示排序", null=True, blank=True, help_text="显示排序")
    remark = models.CharField(max_length=2000, blank=True, null=True, verbose_name="备注", help_text="备注")

    class Meta:
        db_table = table_prefix + 'hades_instance'
        verbose_name = "实例信息"
        verbose_name_plural = verbose_name
        ordering = ('sort',)


class Query(models.Model):
    username = models.CharField(max_length=100, blank=True, null=True, verbose_name="用户名", help_text="用户名")
    instance = models.CharField(max_length=100, blank=True, null=True, verbose_name="实例", help_text="实例")
    instance_id = models.CharField(max_length=100, blank=True, null=True, verbose_name="实例id", help_text="实例id")
    database = models.CharField(max_length=100, blank=True, null=True, verbose_name="数据库", help_text="数据库")
    db_type = models.CharField(max_length=100, blank=True, null=True, verbose_name="db_type", help_text="db_type")
    query_sql = models.CharField(max_length=500, blank=True, null=True, verbose_name="查询语句", help_text="查询语句")
    time_query = models.CharField(max_length=100, blank=True, null=True, verbose_name="查询时间", help_text="查询时间")
    time_cost = models.FloatField(max_length=10, blank=True, null=True, verbose_name="耗时", help_text="耗时")
    sort = models.IntegerField(default=1, verbose_name="显示排序", null=True, blank=True, help_text="显示排序")
    status = models.IntegerField(blank=True, null=True, verbose_name="状态", help_text="状态")

    class Meta:
        db_table = table_prefix + 'hades_query'
        verbose_name = "实例信息"
        verbose_name_plural = verbose_name
        ordering = ('-time_query',)
